<?php

namespace Plugins\AdminBasic\Models;

use App\Models\Attributes\InitData;
use App\Models\Model;
use Carbon\Carbon;
use App\Models\Attributes\CreateSql;
use Plugins\AdminBasic\Enums\BasicRouteEnumType;
use Plugins\AdminBasic\Enums\BasicRouteEnumOpenType;


/**
 * 路由菜单
 * 
 * @property int    $id          ID
 * @property int    $pid         父级
 * @property BasicRouteEnumType $type        类型
 * @property BasicRouteEnumOpenType $open_type   打开类型
 * @property string $title       标题
 * @property string $icon        图标
 * @property string $route       路由
 * @property array  $power_route 权限路由
 * @property int    $weigh       排序
 * @property Carbon $create_time 创建时间
 * @property Carbon $update_time 更新时间
 * @property int    $delete_time 删除时间
 */
#[CreateSql(<<<SQL
CREATE TABLE `sd_route` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `pid` int(11) NOT NULL DEFAULT '0' COMMENT '父级',
  `type` tinyint(1) NOT NULL DEFAULT '1' COMMENT '类型：1=左侧菜单，2=页面操作，3=头部菜单',
  `open_type` tinyint(1) NOT NULL DEFAULT '1' COMMENT '打开类型：1=默认，2=浏览器，3=弹窗',
  `title` varchar(32) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '标题',
  `icon` varchar(32) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '图标',
  `route` varchar(64) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '路由',
  `power_route` json DEFAULT NULL COMMENT '权限路由',
  `weigh` int(11) NOT NULL DEFAULT '1' COMMENT '排序',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '更新时间',
  `delete_time` int(11) DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=70 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='路由菜单'
SQL)]
#[InitData([
    ['id' => 1, 'pid' => 0, 'type' => 1, 'title' => '系统设置', 'icon' => 'Setting', 'route' => '', 'power_route' => '[]', 'weigh' => 1, 'create_time' => '2023-08-16 15:10:02', 'update_time' => '2023-08-16 15:10:02'],
    ['id' => 2, 'pid' => 1, 'type' => 1, 'title' => '路由设置', 'icon' => 'Connection', 'route' => '/admin/plugins/admin-basic/route/lists', 'power_route' => '[]', 'weigh' => 4, 'create_time' => '2023-08-16 15:10:02', 'update_time' => '2023-08-16 15:10:02'],
    ['id' => 3, 'pid' => 1, 'type' => 1, 'title' => '管理员管理', 'icon' => 'Avatar', 'route' => '/admin/plugins/admin-basic/administrators/lists', 'power_route' => '[]', 'weigh' => 3, 'create_time' => '2023-08-16 15:10:02', 'update_time' => '2023-08-16 15:10:02'],
    ['id' => 4, 'pid' => 1, 'type' => 1, 'title' => '角色管理', 'icon' => 'UserFilled', 'route' => '/admin/plugins/admin-basic/role/lists', 'power_route' => '[]', 'weigh' => 2, 'create_time' => '2023-08-16 15:10:02', 'update_time' => '2023-08-16 15:10:02'],
    ['id' => 5, 'pid' => 1, 'type' => 1, 'title' => '配置中心', 'icon' => 'Cpu', 'route' => '/admin/plugins/admin-basic/super_config/lists', 'power_route' => '[]', 'weigh' => 1, 'create_time' => '2023-08-16 15:10:02', 'update_time' => '2023-08-16 15:10:02'],
    ['id' => 6, 'pid' => 1, 'type' => 1, 'title' => '操作日志', 'icon' => 'Document', 'route' => '/admin/plugins/admin-basic/operation_logs/lists', 'power_route' => '[]', 'weigh' => 0, 'create_time' => '2023-08-16 15:10:02', 'update_time' => '2023-08-16 15:10:02'],
])]
class BasicRoute extends Model
{


    protected array $casts = [
        ...self::DEFAULT_CASTS,
        'id' => 'int',
        'pid' => 'int',
        'power_route' => 'array',
        'weigh' => 'int'
    ];

    protected array $fillable = ['pid', 'type', 'open_type', 'title', 'icon', 'route', 'power_route', 'weigh'];

    protected ?string $table = 'basic_route';

    public function getTypeAttribute($value): BasicRouteEnumType
    {
        return BasicRouteEnumType::from($value);
    }

    public function setTypeAttribute($value): void
    {
        $this->attributes['type'] = $value instanceof BasicRouteEnumType ? $value->value : $value;
    }

    public function getOpenTypeAttribute($value): BasicRouteEnumOpenType
    {
        return BasicRouteEnumOpenType::from($value);
    }

    public function setOpenTypeAttribute($value): void
    {
        $this->attributes['open_type'] = $value instanceof BasicRouteEnumOpenType ? $value->value : $value;
    }

    public function parent(): \Hyperf\Database\Model\Relations\BelongsTo
    {
        return $this->belongsTo(self::class, 'pid');
    }
}